package config

import (
	"strconv"
	"strings"
)

func LoadEnvLogConfig() *LogConfig {
	flagStr := LoadEnvironment(EnvLogFlag)
	flag, _ := strconv.Atoi(flagStr)
	if flag == 0 && flagStr == "" {
		return &LogConfig{Level: "debug", Flag: 11}
	}
	return &LogConfig{
		Level: LoadEnvironment(EnvLogLevel),
		Path:  LoadEnvironment(EnvLogPath),
		Flag:  flag,
	}
}

func LoadEnvEtcdConfig() *EtcdConfig {
	etcd := LoadEnvironment(EnvEtcdAddr)
	etcds := strings.Split(etcd, ";")
	host := LoadEnvironment(EnvEtcdHost)
	port := LoadEnvironment(EnvEtcdPort)
	addr := host + port
	return &EtcdConfig{
		Base:           LoadEnvironment(EnvEtcdBase),
		Etcds:          etcds,
		Host:           host,
		Port:           port,
		ServiceAddress: addr,
	}
}

func LoadEnvDBConfig() *DBConfig {
	mic, _ := strconv.Atoi(LoadEnvironment(EnvDBMaxIdelConns))
	moc, _ := strconv.Atoi(LoadEnvironment(EnvDBMaxOpenConns))
	return &DBConfig{
		Addr:         LoadEnvironment(EnvDBAddr),
		UserName:     LoadEnvironment(EnvDBUser),
		Password:     LoadEnvironment(EnvDBPassword),
		Name:         LoadEnvironment(EnvDBName),
		Coding:       LoadEnvironment(EnvDBCoding),
		MaxIdleConns: mic,
		MaxOpenConns: moc,
	}
}

func LoadEnvRedisConfig() *RedisConfig {
	db, _ := strconv.Atoi(LoadEnvironment(EnvRedisDB))
	return &RedisConfig{
		Addr:     LoadEnvironment(EnvRedisAddr),
		PassWord: LoadEnvironment(EnvRedisPassword),
		DB:       db,
	}
}
